﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Base;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using Model;
using WebApp.Filter;
using WebApp.Models;

namespace WebApp.Controllers
{

    [SignFilter]
    public class HomeController : Controller
    {

        EFContext _ef;

        public HomeController(EFContext ef)
        {
            this._ef = ef;
        }



        
        public IActionResult Index()
        {
            //for (int i = 0; i < 2000; i++)
            //{
            //    sys_admin sa = new sys_admin();
            //    sa.admin_name = "name" + i;
            //    sa.admin_password = "password";
            //    _ef.Add(sa);
            //}
            //_ef.SaveChanges();
            return View();
        }


        public IActionResult GetMenu()
        {
            return Json(new MyJsResult(0,"ok",null));
        }



        [NoSign]
        public IActionResult Login()
        {
            string msg =Request.Query["msg"];
            ViewBag.msg = msg;
            return View();
        }


        [NoSign]
        [HttpPost]
        public async Task<IActionResult> LoginAction(string username,string password)
        {
            sys_admin sa = _ef.sys_admin.FirstOrDefault(x => x.admin_name == username);
            if (sa!=null)
            {
                if (sa.admin_status!=1)
                {
                    return RedirectToAction("Login", new { msg = "用户已停用！" });
                }

                if (sa.admin_password!=password)
                {
                    return RedirectToAction("Login", new { msg = "用户名不存在或密码错误！" });
                }
                else
                {
                    
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);//一定要声明AuthenticationScheme
                    identity.AddClaim(new Claim(SystemConst.CURRENT_ADMIN_ID, sa.id.ToString()));
                    identity.AddClaim(new Claim(SystemConst.CURRENT_ADMIN_UNAME, sa.admin_name.ToString()));
                    var claimsPrincipal = new ClaimsPrincipal(identity);
                    await HttpContext.SignInAsync(identity.AuthenticationType,
                                                  claimsPrincipal);
                    sa.admin_login_time = DateTime.Now;
                    _ef.Update(sa);
                    _ef.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            else
            {
                await HttpContext.ChallengeAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                return RedirectToAction("Login", new { msg = "用户名不存在！" });
                //return RedirectToAction("Login", new { msg = "用户名不存在或密码错误！" });
            }
        }


        public async Task<IActionResult> LoginOut()
        {
            await HttpContext.SignOutAsync();
            return RedirectToAction("Login");
        }

        public IActionResult Welcome()
        {
            return View();
        }



        [NoSign]
        public IActionResult Error()
        {
            return Content("拒绝访问!");
        }


    }
}
